#!/usr/bin/perl
#
# Clean some records
#
use Counter;
use Getopt::Long;

$limit = 10;

GetOptions("limit=i" => \$limit,
	   "random" => \$random,
	   "watch=s" => \$watch,
	   );

$file = shift;
$field = shift;
$value = shift;

  
$file = Counter::openfile($file, O_RDWR);
if ($random && !$value) {
    @recs = $file->select("f_key is not null order by rand() limit $limit");
} else {
    die "Usage: $0 file field value\n" if !$value;
    @recs = $file->select("$field like '$value' order by rand() limit $limit");
}
warn "Found ", @recs + 0, " records\n";
for $rec (@recs) {
    $key = $rec->key();
    $clean = $file->getclean($key);
    if ($watch) {
	$before = $clean->{$watch};
    }
    $rec->clean();
    $clean = $file->getclean($key);
    if ($watch) {
	$after = $clean->{$watch};
    }
    if ($before ne $after) {
	warn "$key: Changed $watch from $before to $after\n";
    }
}

    
